﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Volo.Abp.Modularity;
using Volo.Abp.ObjectExtending;
using Volo.Abp.SqlSugarCore.ExtensionEntity;
using Volo.Abp.Modularity;

namespace Volo.Abp.Modularity
{
    public static class ExtensionEntityPropertyLoad
    {
        public static void LoadExtensionEntity(this ServiceConfigurationContext context, Assembly modularAssembly)
        {
            var extensionDtoList = modularAssembly.GetTypes().Where(e => e.GetCustomAttribute<ExtensionEntityAttribute>() != null);
            foreach (var item in extensionDtoList)
            {
                var extAttr = item.GetCustomAttribute<ExtensionEntityAttribute>();

                var itemProList = item.GetProperties();

                ObjectExtensionManager.Instance
                      .AddOrUpdate(new Type[] { extAttr.EntityType }, options =>
                       {
                           foreach (var item in itemProList)
                           {
                               options.AddOrUpdateProperty(item.PropertyType, item.Name, propertyInfo =>
                               {
                                   propertyInfo.CheckPairDefinitionOnMapping = false;
                               });
                           }
                       });
            }
        }
    }
}
